﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using Tasks247.Model;

namespace Tasks247.Data
{
    public class TasksContext : DbContext, ITasksRepository
    {

        public void Delete(int id)
        {
            var task = Tasks.First(item => item.Id == id);
            Tasks.Remove(task);
            SaveChanges();
        }

        public List<TaskEntity> Read()
        {
            return Tasks.ToList();
        }

        public void Update(TaskEntity task)
        {            
            Tasks.AddOrUpdate(task);
            SaveChanges();
        }

        public int Create(string name, string notes)
        {
            var tasks = new[] {new TaskEntity {Name = name, Notes = notes}};
            Tasks.AddOrUpdate(tasks);
            SaveChanges();
            return tasks[0].Id;
        }

        public DbSet<TaskEntity> Tasks { get; set; }


    }
}